/**
 * @(#)Logic.java
 *
 * Copyright: Copyright (c) 2003 Carnegie Mellon University
 *
 * This program is the implementation of the original system in the Assignment 2 of "Architectures
 * of Software Systems" class. For the details of each classes, please refer to the provided javadoc
 * documents. You will also find some useful information such as implementation decisions, coding
 * standards, and naming conventions in the README file.
 */

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Calendar;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * This class represents a logic node which is responsible for providing high-level data access
 * methods to clients using RMI mechanism. It depends on the functionalities of data node
 * which is made available also using RMI mechanism, thus acting as a "bridge" between client nodes
 * and the data node.
 *
 * @author Jung Soo Kim
 * @version 1.0
 */
public class Logging extends UnicastRemoteObject implements RILogging{

		
    private static final String LOGIC_NAME = "Logic";    
    private static final String LOGGING_NAME = "Logging";     
    protected BufferedWriter objLoggingFile;
        
    public Logging(String sDataName)
            throws RemoteException, NotBoundException, MalformedURLException {
         // Get the remote reference to data node.
         //this.rmiDataNode = (RIData) Naming.lookup(sDataName);
    	/*try{
    	this.objLoggingFile = new BufferedWriter(new FileWriter(".\\Log.txt"));
    	}
    	catch(Exception e)
    	{
    		
    	}*/
     }   
    
    public void writeALog(String sUID, String sCID){
    	//Calendar calendar = Calendar.getInstance();
    	//SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    	Date dDay = new Date();
    	
    	System.out.print("\n\rLogging Called...");
    	
    	try{
    		this.objLoggingFile = new BufferedWriter(new FileWriter(".\\Log.txt",true));
    		//this.objLoggingFile.write(dateFormat.format("Time : " + calendar.getTime()) + "User ID : " + sUID + "Command ID : " + sCID);
    		this.objLoggingFile.write("[Time] : " + dDay + "   [User ID] : " + sUID + "   [Command ID] : " + sCID);
    		
    		//this.objLoggingFile.write("test!!!");
    		this.objLoggingFile.newLine();
    		this.objLoggingFile.close();
    	}
    	catch(Exception e)
    	{
    		
    	}
    	
    }
    
    public void CloseALog(){
    	    	
    	try{
    		objLoggingFile.close();    		    		    		
    	}
    	catch(Exception e)
    	{
    		
    	}
    	
    }
    
    
    //FileWriter LogFile = new FileWriter(new File(".\\Log.txt"));
    
    /**
     * Creates a logic node and starts it. Two parameters are expected, the first one as data node
     * name and the second one as logic node name.
     *
     * @param args array of input parameters
     */
    public static void main(String args[]) throws Exception{   	   	
    	
    	// Check the number of parameters.
        if (args.length != 0) {
            System.out.println("Incorrect number of parameters");
            System.out.println("Usage: java Logging");
            System.exit(1);
        }

        try {
            // Create a Logging node and make it available through RMI.
            Logging objLogging = new Logging(LOGIC_NAME);
            Naming.rebind(LOGGING_NAME, objLogging);
            System.out.println("Logging node is ready to serve.");

            // Wait for user interrupt.
            System.out.println("Press Enter to terminate.");
            System.in.read();
            
            objLogging.CloseALog();
            
            // Clean up and finish.
            Naming.unbind(LOGIC_NAME);
            
            System.out.println("Logic node is leaving, bye. Press ctrl-c if the delay is too long.");
        }
        catch (java.rmi.ConnectException e) {
            // Show error message and quit.
            System.err.println("Java RMI error: check if rmiregistry is running.");
            System.exit(1);
	}
        catch (java.rmi.NotBoundException e) {
            // Show error message and quit.
            System.err.println("Java RMI error: check if data node is running.");
            System.exit(1);
        }
        catch (Exception e) {
            // Dump the exception information for debugging.
            System.out.println("Unexpected exception at " + LOGIC_NAME);
            e.printStackTrace();
            System.exit(1);
        }
    }
}
